/**
 * 
 */
package fr.umlv.m2.ig.view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JComponent;

import fr.umlv.m2.ig.model.Train;

/**
 * This class defines a component for trains.
 * 
 * @author Gambouele Bertrand.
 * @author Viravout Alexandre.
 */
@SuppressWarnings("serial")
public class TrainComponent extends JComponent {

	/**
	 * Train radius.
	 */
	public static int radius = 3;
	
	/**
	 * The selected line.
	 */
	public static String selectedLine = null;
	
	/**
	 * Composite for drawing.
	 */
	private final static Composite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f);
	
	/**
	 * {@code Train} object associated to the specified component.
	 */
	private final Train train;
	
	/**
	 * Creates a new {@code TrainComponent}.
	 * 
	 * @param train train associated to the component.
	 */
	public TrainComponent(final Train train) {
		this.train = train;
		this.setToolTipText(train.getId());
	}
	
	/**
	 * Returns the train associated to this component.
	 * 
	 * @return {@code Train} object associated to this component.
	 */
	public Train getTrain() {
		return this.train;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		if (TrainComponent.selectedLine == null || TrainComponent.selectedLine.equals(this.train.getLine())) {
			Graphics2D g2 = (Graphics2D)g;
			
			Color c = g2.getColor();
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g2.setColor(Color.RED);
			g2.setComposite(TrainComponent.composite);
			g2.fillOval(0, 0, getWidth(), getHeight());
			g2.setColor(c);
		}
	}
}
